#include "McuInit.h"
#include "Interrupt.h"
#include "app.h"
extern	u8	RxdCnt;
extern	u8	RxdFlag;
extern	u8	RxdData[8];

extern	u8 	Time2msFlag;

extern 	u8 	Flag_IR_Handle;

extern 	u8	IRDataArray[4];
extern 	u8	IR_ReadBitCnt;
extern 	u8	IR_High_Cnt;
extern 	u8	IR_Low_Cnt;

extern u8		IRauxDataArray[4];
extern u8		IRaux_ReadBitCnt;
extern u8		Foot_IRaux_High_Cnt;
extern u16	Foot_IRaux_Low_Cnt;

extern u8		OverTimeCnt;
extern u8		IRHandleCnt;

extern u8		IRData,IRauxData;
extern u8		IRDataCnt,IRauxDataCnt;

u8 	Time400usCnt;
void GPIO_Interrupt(void) interrupt 7
{
	switch(PIF)
	{
		case PIF0:
			break;
		case PIF1:
			break;
		case PIF2:
			break;
		case PIF3:
			break;
		case PIF4:
			break;
		case PIF5:
			break;
		case PIF6:
			break;
		case PIF7:
			break;
		default:
			break;
	}
	PIF = 0;
}
/************************************************************************************************************
*    TIMER 0 interrupt subroutine
************************************************************************************************************/
void Timer0_ISR (void) interrupt 1  //interrupt address is 0x000B
{
    	TH0 = Timer0_TH0_Init_400us;
    	TL0 = Timer0_TL0_Init_400us;    
    	TF0 = 0;
	Time400usCnt++;
	if(!Flag_IR_Handle)
	{
		if(IRMain_Pin)
		{
			if(IR_High_Cnt < 250) 
			{
				IR_High_Cnt ++;
				if(IR_ReadBitCnt == 8)
				{
					IR_ReadBitCnt = 0;
					IRDataArray[IRDataCnt] = IRData;
					IRDataCnt ++;						
					IRData = 0;
				}
			}
		}
		else
		{
			OverTimeCnt = 0;
			if(IR_High_Cnt) 
			{
				IR_ReadBitCnt ++;
				IRData <<= 1;
				if(IR_High_Cnt > IR_BIT1_MAX_CNT)		// 4ms/0.208us=19
					IR_ReadBitCnt = 0;
				else if(IR_High_Cnt >= IR_BIT1_EFFECT_CNT)
					IRData |= 0x01;
					
				IR_High_Cnt = 0;
			}
		}

		if(IR_Foot_Pin)
		{
			if(Foot_IRaux_High_Cnt < 250) 
			{
				Foot_IRaux_High_Cnt ++;
				if(IRaux_ReadBitCnt == 8)
				{
					IRaux_ReadBitCnt = 0;
					IRauxDataArray[IRauxDataCnt] = IRauxData;
					IRauxDataCnt ++;
					IRauxData = 0;
				}	
			}
		}
		else
		{
			OverTimeCnt = 0;
			Foot_IRaux_Low_Cnt++;
			if(Foot_IRaux_High_Cnt) 
			{
				IRaux_ReadBitCnt ++;
				IRauxData <<= 1;
				if(Foot_IRaux_High_Cnt > IR_BIT1_MAX_CNT)		// 4ms/0.208us=19
					IRaux_ReadBitCnt = 0;
				else if(Foot_IRaux_High_Cnt >= IR_BIT1_EFFECT_CNT)
					IRauxData |= 0x01;
					
				Foot_IRaux_High_Cnt = 0;
			}
		}
	}
	if(Time400usCnt >= 5)
	{
		Time400usCnt = 0;
		Time2msFlag = 1;
	}
}
/*****************************************
*    TIMER 3 interrupt subroutine
******************************************/
void Timer3_ISR (void) interrupt 16 
{
	Time2msFlag = 1;   
}

/*****************************************
*    UART 0 interrupt subroutine
******************************************/
void Uart0_ISR (void) interrupt 4 
{
	if (RI)
	{   
		RI = 0;
		RxdData[RxdCnt] = SBUF;
		if(RxdData[0] == 0xaa)
		{
			RxdCnt ++;
			if(RxdCnt > 6)
			{
				RxdCnt = 0;
				RxdFlag = 1;
			}
		}
	}
	if (TI)
	{
		TI = 0;        // Clear TI (Transmit Interrupt).
		TxdCnt ++;
		if(TxdCnt < 8)
			SBUF = TxdData[TxdCnt];
	}
}

/*****************************************
*    UART 1 interrupt subroutine
******************************************/
void Uart1_ISR (void) interrupt 15 
{
	if (RI_1)
	{   
		RI_1 = 0;
		RxdData[RxdCnt] = SBUF_1;
		if(RxdData[0] == 0xaa)
		{
			RxdCnt ++;
			if(RxdCnt > 6)
			{
				RxdCnt = 0;
				RxdFlag = 1;
			}
		}
	}
	if (TI_1)
		TI_1 = 0;                                       // Clear TI (Transmit Interrupt).
}

